#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    const int inf = 0x3f3f3f3f;
    bool hasNagetiveCircle(vector<vector<int>> &edges, int n) {
        vector<vector<int>> dp(n, vector<int>(n, inf));
        for (auto edge : edges) {
            int u = edge[0], v = edge[1], w = edge[2];
            dp[u][v] = w;
        }
        for (int i = 0; i < n; ++i) {
            dp[i][i] = 0;
        }
        for (int k = 0; k < n; ++k) {
            for (int i = 0; i < n; ++i) {
                if (i == k) continue;
                for (int j = 0; j < n; ++j) {
                    if (j == k) continue;
                    if (dp[i][k] < inf && dp[k][j] < inf)
                        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            if (dp[i][i] < 0) return true;
        }
        return false;
    }

};
int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> edges;
    while (m--) {
        int u, v, w;
        cin >> u >> v >> w;
        edges.push_back(vector<int>{u, v, w});
    }
    cout << (Solution().hasNagetiveCircle(edges, n) ? 1 : 0) << endl;
}